package com.retoc.oawx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retoc.oawx.domain.Dept;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.mapper.UserMapper;
import com.retoc.oawx.service.DeptService;
import com.retoc.oawx.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author retoc
* @description 针对表【tb_dept】的数据库操作Service实现
* @createDate 2022-05-01 20:00:13
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
    implements DeptService{

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Dept> searchAllDept() {
        return deptMapper.searchAllDept();
    }

    @Override
    public int insertDept(String deptName) {
        ArrayList<HashMap> hashMaps = deptMapper.searchDeptMembers(deptName);
        if (!hashMaps.isEmpty()){
            throw new OAException("部门已存在");
        }
        int row = deptMapper.insertDept(deptName);
        if (row != 1) {
            throw new OAException("部门添加失败");
        }
        return row;
    }

    @Override
    public void deleteDeptById(int id) {
        //查询部门是否有数据
        long count = userMapper.searchUserCountInDept(id);
        if (count > 0) {
            throw new OAException("部门中有员工，无法删除部门");
        } else {
            int row = deptMapper.deleteDeptById(id);
            if (row != 1) {
                throw new OAException("部门删除失败");
            }
        }
    }

    @Override
    public void updateDeptById(Dept entity){
        int row=deptMapper.updateDeptById(entity);
        if (row != 1) {
            throw new OAException("部门修改失败");
        }
    }

}




